package com.web.exiaodao.remote.service.impl;

import com.web.exiaodao.business.entity.ExchangeProducts;
import com.web.exiaodao.remote.vo.ExchangeProductsVo;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.web.exiaodao.business.repository.ExchangeProductsRepository;
import com.web.exiaodao.remote.service.ApiExchangeProductsService;
import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.util.jpa.DynamicSpecifications;
import com.web.webstart.base.util.jpa.SearchFilter;
import com.web.webstart.base.service.impl.BaseService;
import com.web.webstart.base.util.jpa.SearchFilter.Operator;
import com.web.webstart.base.util.result.XaResult;
import com.web.webstart.base.util.XaUtil;

/**
 * @author: zhangl
 * @times: 2016-11-28 21:36:08
 * 类的说明：前端APIExchangeProducts接口实现类
 **/
@Service("ApiExchangeProductsService")
@Transactional(readOnly = false)
public class ApiExchangeProductsServiceImpl extends BaseService<ExchangeProducts> implements ApiExchangeProductsService{

	private static final Logger LOGGER = Logger.getLogger(ApiExchangeProductsServiceImpl.class);

	@Autowired
	ExchangeProductsRepository exchangeProductsRepository;
	
	@Override
	public XaResult<ExchangeProductsVo> findOne(Long tId) throws BusinessException {
		ExchangeProducts obj = exchangeProductsRepository.findByIdAndStatusNot(tId,XaConstant.Status.delete);
		XaResult<ExchangeProductsVo> xr = new XaResult<ExchangeProductsVo>();
		if (XaUtil.isNotEmpty(obj)) {
			xr.setObject(JSON.parseObject(JSON.toJSONString(obj),ExchangeProductsVo.class));
			xr.getObject().setId(obj.getId());
			LOGGER.info("查询单个实体----->ExchangeProducts");
		} else {
			xr.error(XaConstant.Message.object_not_find);
			LOGGER.info(XaConstant.Message.object_not_find);
			throw new BusinessException(XaConstant.Message.object_not_find);
		}
		return xr;
	}

	@Override
	public XaResult<ExchangeProductsVo> findLastOne() throws BusinessException {
		XaResult<ExchangeProductsVo> xr = new XaResult<>();
		Map<String, SearchFilter> filters = new HashMap<>();
			filters.put("status", new SearchFilter("status", Operator.NE, XaConstant.Status.delete));
			List<ExchangeProducts> entitys = exchangeProductsRepository.findAll(DynamicSpecifications
				.bySearchFilter(filters.values(), ExchangeProducts.class), new Sort(
				new Sort.Order[]{
				new Sort.Order(Sort.Direction.DESC, "createTime"),
				new Sort.Order(Sort.Direction.DESC, "id")
				}));
				if (entitys == null || entitys.size() == 0) {
				xr.error("暂无信息");
				} else {
				xr.setObject(JSON.parseObject(JSON.toJSONString(entitys.get(0)), ExchangeProductsVo.class));
				xr.getObject().setId(entitys.get(0).getId());
				LOGGER.info("查询单个实体----->ExchangeProducts");
			}
		return xr;
	}

	@Override
	public XaResult<List<ExchangeProductsVo>> findListNEStatusByFilter(
			Integer status, Map<String, Object> filterParams, Pageable pageable)
			throws BusinessException {
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		if(status == null){// 默认显示非删除的所有数据
			status = XaConstant.Status.delete;
		}
		filters.put("status", new SearchFilter("status", Operator.NE, status));
		Page<ExchangeProducts> page = exchangeProductsRepository.findAll(DynamicSpecifications
				.bySearchFilter(filters.values(), ExchangeProducts.class), pageable);
		XaResult<List<ExchangeProductsVo>> xr = new XaResult<List<ExchangeProductsVo>>();
		xr.setObject(JSON.parseArray(JSON.toJSONString(page.getContent()), ExchangeProductsVo.class));
		for(int i=0;i<page.getContent().size();i++){
			xr.getObject().get(i).setId(page.getContent().get(i).getId());
		}
		return xr;
	}

	@Override
	public XaResult<List<ExchangeProductsVo>> findListEQStatusByFilter(
			Integer status, Map<String, Object> filterParams, Pageable pageable)
			throws BusinessException {
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		if(status == null){// 默认显示正常数据
			status = XaConstant.Status.valid;
		}
		filters.put("status", new SearchFilter("status", Operator.EQ, status));
		Page<ExchangeProducts> page = exchangeProductsRepository.findAll(
				DynamicSpecifications.bySearchFilter(filters.values(),ExchangeProducts.class), pageable);
		XaResult<List<ExchangeProductsVo>> xr = new XaResult<List<ExchangeProductsVo>>();
		xr.setObject(JSON.parseArray(JSON.toJSONString(page.getContent()), ExchangeProductsVo.class));
		for(int i=0;i<page.getContent().size();i++){
			xr.getObject().get(i).setId(page.getContent().get(i).getId());
		}
		return xr;
	}

	@Override
	public XaResult<ExchangeProductsVo> multiOperate(String modelIds,
			Integer status) throws BusinessException {
		XaResult<ExchangeProductsVo> xr = new XaResult<ExchangeProductsVo>();
		if(status == null){
			status = XaConstant.Status.delete;
		}
		if(modelIds != null){
			String[] ids = modelIds.split(",");
			for(String id : ids){
				ExchangeProducts obj = exchangeProductsRepository.findByIdAndStatusNot(Long.parseLong(id),status);
				if (XaUtil.isNotEmpty(obj)) {
					obj.setStatus(status);
					obj = exchangeProductsRepository.save(obj);
					xr.setObject(JSON.parseObject(JSON.toJSONString(obj), ExchangeProductsVo.class));
					xr.getObject().setId(obj.getId());
				}
			}
		}
		LOGGER.info("更改数据状态----->ExchangeProducts");
		return xr;
	}

	@Override
	public XaResult<ExchangeProductsVo> createModel(ExchangeProducts model)
			throws BusinessException {
		XaResult<ExchangeProductsVo> xr = new XaResult<ExchangeProductsVo>();
		ExchangeProducts obj = exchangeProductsRepository.save(model);
		xr.setObject(JSON.parseObject(JSON.toJSONString(obj), ExchangeProductsVo.class));
		xr.getObject().setId(obj.getId());
		LOGGER.info("新增数据成功----->ExchangeProducts");
		return xr;
	}

	@Override
	public XaResult<ExchangeProductsVo> updateModel(ExchangeProducts model)
			throws BusinessException {
		ExchangeProducts obj = exchangeProductsRepository.findOne(model.getId());
		XaResult<ExchangeProductsVo> xr = new XaResult<ExchangeProductsVo>();
		if (XaUtil.isNotEmpty(obj)) {
					obj.setProductName(XaUtil.isNotEmpty(model.getProductName()) ? model.getProductName() : obj.getProductName());
					obj.setNeedCoins(XaUtil.isNotEmpty(model.getNeedCoins()) ? model.getNeedCoins() : obj.getNeedCoins());
					obj.setProductImg(XaUtil.isNotEmpty(model.getProductImg()) ? model.getProductImg() : obj.getProductImg());
					obj.setInventory(XaUtil.isNotEmpty(model.getInventory()) ? model.getInventory() : obj.getInventory());
					obj.setExchangedNum(XaUtil.isNotEmpty(model.getExchangedNum()) ? model.getExchangedNum() : obj.getExchangedNum());
					obj.setProductIntro(XaUtil.isNotEmpty(model.getProductIntro()) ? model.getProductIntro() : obj.getProductIntro());
					obj.setProductProvider(XaUtil.isNotEmpty(model.getProductProvider()) ? model.getProductProvider() : obj.getProductProvider());
				obj = exchangeProductsRepository.save(obj);
			xr.setObject(JSON.parseObject(JSON.toJSONString(obj), ExchangeProductsVo.class));
			xr.getObject().setId(obj.getId());
		} else {
			xr.error(XaConstant.Message.object_not_find);
		}
		LOGGER.info("编辑数据成功----->ExchangeProducts");
		return xr;
	}

}
